package com.cn.laoluobase.controller.demo;

import com.cn.laoluobase.jo.BusinessException;
import com.cn.laoluobase.jo.MyHttpEnum;
import com.cn.laoluobase.jo.ResultsObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/post")
public class Post {

    private final static Logger logger = LoggerFactory.getLogger("/Post"); // 创建Logger
    /**
     * ResultsObj返回示例1-标准方法ResultsObj.success、fail等
     * @return {
     *   "code": 1000,
     *   "data": 111111,
     *   "msg": "成功"
     * }
     * */
    @PostMapping("/results1")
    public ResultsObj post5(@RequestBody HashMap map){
        // @RequestParam 定义参数
        Integer i =10,J=0;
        try{
        J = i/(i -10);
        }
        catch (Exception e){
            throw  new BusinessException(MyHttpEnum.NOTZERO.name());
//            throw BusinessException.byName(MyHttpEnum.NOTZERO.name());
        }

        return ResultsObj.success(J);
    }


    /**
     * ResultsObj返回示例2-根据code返回枚举类
     * @return {
     *   "code": 404,
     *   "msg": "未找资资源"
     * }
     * */
    @PostMapping("/results2")
    public ResultsObj post6(@RequestBody HashMap map){
        return  ResultsObj.byCode(405);
    }

    /**
     * ResultsObj返回示例3-根据name返回枚举类
     * @return {
     *   "code": 999,
     *   "msg": "失败"
     * }
     * */
    @PostMapping("/results3")
    public ResultsObj post7(@RequestBody HashMap map){
        return  ResultsObj.byName(MyHttpEnum.FAIL.name());
    }
}
